package com.logistics.service.impl;

import com.logistics.common.Result;
import com.logistics.entity.LogisticsOrder;
import com.logistics.entity.Warehouse;
import com.logistics.entity.WarehouseStorage;
import com.logistics.mapper.WarehouseStorageMapper;
import com.logistics.service.LogisticsOrderService;
import com.logistics.service.WarehouseService;
import com.logistics.service.WarehouseStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class WarehouseStorageServiceImpl implements WarehouseStorageService {
    @Autowired
    private WarehouseStorageMapper warehouseStorageMapper;
    
    @Autowired
    private WarehouseService warehouseService;
    
    @Autowired
    private LogisticsOrderService logisticsOrderService;

    @Override
    @Transactional
    public void create(WarehouseStorage storage) {
        // 检查仓库是否存在
        Result<Warehouse> warehouseResult = warehouseService.getById(storage.getWarehouseId());
        if (warehouseResult.getCode() != 200) {
            throw new RuntimeException("仓库不存在");
        }
        
        // 检查订单是否存在
        Result<LogisticsOrder> orderResult = logisticsOrderService.getByOrderNo(storage.getOrderNo());
        if (orderResult.getCode() != 200 || orderResult.getData() == null) {
            throw new RuntimeException("订单不存在");
        }
        
        // 设置创建时间
        storage.setCreateTime(LocalDateTime.now());
        
        // 插入存储记录
        warehouseStorageMapper.insert(storage);
    }

    @Override
    public WarehouseStorage getById(Long id) {
        return warehouseStorageMapper.selectById(id);
    }

    @Override
    public List<WarehouseStorage> listByWarehouseId(Long warehouseId) {
        return warehouseStorageMapper.selectByWarehouseId(warehouseId);
    }

    @Override
    public List<WarehouseStorage> listByOrderNo(String orderNo) {
        return warehouseStorageMapper.selectByOrderNo(orderNo);
    }

    @Override
    public List<WarehouseStorage> list() {
        return warehouseStorageMapper.selectAll();
    }

    @Override
    public Result<List<WarehouseStorage>> search(String orderNo, Long warehouseId, String status) {
        try {
            List<WarehouseStorage> storages = warehouseStorageMapper.search(orderNo, warehouseId, status);
            return Result.success(storages);
        } catch (Exception e) {
            return Result.error("搜索失败：" + e.getMessage());
        }
    }

    @Override
    @Transactional
    public void updateStatus(Long id, String status) {
        // 检查存储记录是否存在
        WarehouseStorage storage = warehouseStorageMapper.selectById(id);
        if (storage == null) {
            throw new RuntimeException("存储记录不存在");
        }
        
        // 更新状态
        warehouseStorageMapper.updateStatus(id, status);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 检查存储记录是否存在
        WarehouseStorage storage = warehouseStorageMapper.selectById(id);
        if (storage == null) {
            throw new RuntimeException("存储记录不存在");
        }
        
        // 删除存储记录
        warehouseStorageMapper.deleteById(id);
    }
} 